package com.tencent.trtc.apiexample;

import androidx.appcompat.app.AppCompatActivity;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.tencent.trtc.apiexample.ui.dashboard.DashboardFragment;

import com.alibaba.fastjson.*;

public class LoginActivity extends AppCompatActivity {
    private EditText mLogin_UserName;
    private EditText mLogin_UserPwd;
    private ImageView mLogin_Confirm;
    private Button mLogin_Register;

    public static String PATH = "http://120.78.210.235:8080/myWeb";
    public static String USERNAME = "";
    public static JSONObject json = new JSONObject();
    private String password = "";
    private String info = "";
    private String reginfo = "";
    private String result="";
    private static Handler handler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mLogin_UserName=findViewById(R.id.Login_UserName);
        mLogin_UserPwd=findViewById(R.id.Login_UserPwd);
        mLogin_Confirm=findViewById(R.id.Login_Confirm);
        mLogin_Register=findViewById(R.id.Login_Register);

        Intent intent = getIntent();
        reginfo = intent.getStringExtra("reginfo");
        if(reginfo != null)
            Toast.makeText(LoginActivity.this,reginfo,Toast.LENGTH_LONG).show();

        mLogin_Confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new MyThread()).start();
            }
        });

        mLogin_Register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
    }
    class MyThread implements Runnable {

        @Override
        public void run() {
            USERNAME = mLogin_UserName.getText().toString();
            password = mLogin_UserPwd.getText().toString();
            String path = PATH + "/login?username=" + USERNAME + "&password=" + password;
            info = WebServiceGet.executeHttpGet(path);
            path = PATH + "/finddata?username="+USERNAME;
            result = WebServiceGet.executeHttpGet(path);
            json = JSON.parseObject(result);
            if(info.equals("登录成功")){
                Intent intent=new Intent(LoginActivity.this, NavActivity.class);
                startActivity(intent);
            }else{
                Looper.prepare();
                Toast.makeText(LoginActivity.this,"登录失败，请重试！",Toast.LENGTH_LONG).show();
                Looper.loop();
            }
        }
    }
}

